package com.gd.coreservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.coreservice.entity.UserLeave;
import com.gd.coreservice.entity.Users;
import com.gd.coreservice.mapper.UserLeaveMapper;
import com.gd.coreservice.service.UserLeaveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@Service
public class UserLeaveServiceImpl extends ServiceImpl<UserLeaveMapper, UserLeave> implements UserLeaveService {

    @Override
    public boolean insertLeave(String uid, String gid, String lmessage) {
        UserLeave userLeave = new UserLeave();
        userLeave.setUid(uid);
        userLeave.setGid(gid);
        userLeave.setMessage(lmessage);
        int result = 0;
        try {
            result = baseMapper.insert(userLeave);
            if (0!=result){
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }

    @Override
    public Integer deleteLeave(String lid,String uid) {
        QueryWrapper<UserLeave> userLeaveQueryWrapper = new QueryWrapper<>();
        userLeaveQueryWrapper.eq("lid",lid)
                .eq("uid",uid);
        int delete = baseMapper.delete(userLeaveQueryWrapper);
        return delete;
    }

    @Override
    public Map<String, Object> getUserLeavePageData(Integer pageNum, Integer pageSize, String gid) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        Page<UserLeave> page = new Page<>(pageNum,pageSize);
        QueryWrapper<UserLeave> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("gid",gid);
        IPage<UserLeave> usersIPage = baseMapper.selectPage(page, objectQueryWrapper);

        objectObjectHashMap.put("data",usersIPage.getRecords());
        objectObjectHashMap.put("total",usersIPage.getTotal());
        return objectObjectHashMap;
    }

    @Override
    public Map<String, Object> getUserLeavePageDataUid(Integer pageNum, Integer pageSize, String uid) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        Page<UserLeave> page = new Page<>(pageNum,pageSize);
        QueryWrapper<UserLeave> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("uid",uid);
        IPage<UserLeave> usersIPage = baseMapper.selectPage(page, objectQueryWrapper);

        objectObjectHashMap.put("data",usersIPage.getRecords());
        objectObjectHashMap.put("total",usersIPage.getTotal());
        return objectObjectHashMap;
    }
}
